package com.cskaoyan.gateway.controller.user;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.commons.tool.utils.CookieUtil;
import com.mall.user.IKaptchaService;
import com.mall.user.IUserLoginService;
import com.mall.user.IUserRegisterService;
import com.mall.user.annotation.Anoymous;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dto.*;
import com.mall.user.intercepter.TokenIntercepter;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@RestController
@RequestMapping("user")
public class UserController {

    @Reference(timeout = 3000, retries = 0, check = false)
    private IKaptchaService iKaptchaService;
    @Reference(timeout = 3000, retries = 0, check = false)
    private IUserLoginService userLoginService;
    @Reference(timeout = 3000, retries = 0, check = false)
    private IUserRegisterService userRegisterService;

    /**
     * 用户登录
     * @return 登录成功与否
     */
    @Anoymous
    @PostMapping("login")
    public ResponseData login(@RequestBody Map<String, String> map, HttpServletRequest httpServletRequest, HttpServletResponse response) {
        UserLoginRequest request = new UserLoginRequest();
        request.setUserName(map.get("userName"));
        request.setPassword(map.get("userPwd"));
        // 验证码校验错误返回提示
        String captcha = map.get("captcha");
        KaptchaCodeResponse kaptchaCodeResponse = verifyImageCode(captcha, httpServletRequest);
        if (!kaptchaCodeResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode()))
            return new ResponseUtil<>().setErrorMsg(kaptchaCodeResponse.getMsg());
        UserLoginResponse loginResponse = userLoginService.login(request);
        if (loginResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            Cookie cookie = CookieUtil.genCookie(TokenIntercepter.ACCESS_TOKEN, loginResponse.getToken(), "/", 1800);
            cookie.setHttpOnly(true);
            response.addCookie(cookie);
            return new ResponseUtil<>().setData(loginResponse);
        }

        return new ResponseUtil<>().setErrorMsg(loginResponse.getMsg());
    }

    /**
     * 验证用户登录
     * @return 是否已登录
     */
    @GetMapping("login")
    public ResponseData verifyLogin(HttpServletRequest request) {
        String userInfo = (String) request.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject object = JSON.parseObject(userInfo);

        return new ResponseUtil<>().setData(object);
    }

    /**
     * 用户注册
     * @param map 用户信息
     * @param httpServletRequest 注册请求
     * @return 注册响应
     */
    @Anoymous
    @PostMapping("register")
    public ResponseData register(@RequestBody Map<String, String> map, HttpServletRequest httpServletRequest) {
        String userName = map.get("userName");
        String userPwd = map.get("userPwd");
        String email = map.get("email");
        String captcha = map.get("captcha");
        // 验证码校验错误返回提示
        KaptchaCodeResponse kaptchaCodeResponse = verifyImageCode(captcha, httpServletRequest);
        if (!kaptchaCodeResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode()))
            return new ResponseUtil<>().setErrorMsg(kaptchaCodeResponse.getMsg());

        UserRegisterRequest userRegisterRequest = new UserRegisterRequest();
        userRegisterRequest.setUserName(userName);
        userRegisterRequest.setUserPwd(userPwd);
        userRegisterRequest.setEmail(email);
        UserRegisterResponse userRegisterResponse = userRegisterService.userRegister(userRegisterRequest);
        if (userRegisterResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode()))
            return new ResponseUtil<>().setData(null);
        return new ResponseUtil<>().setErrorMsg(userRegisterResponse.getMsg());
    }

    /**
     * 校验验证码
     * @param captcha 验证码
     * @param request 请求
     * @return 校验的响应
     */
    private KaptchaCodeResponse verifyImageCode(String captcha, HttpServletRequest request) {
        KaptchaCodeRequest kaptchaCodeRequest = new KaptchaCodeRequest();
        String uuid = CookieUtil.getCookieValue(request, "kaptcha_uuid");
        kaptchaCodeRequest.setCode(captcha);
        kaptchaCodeRequest.setUuid(uuid);
        KaptchaCodeResponse response = iKaptchaService.validateKaptchaCode(kaptchaCodeRequest);
        return response;
    }
    /**
    * @Description: 退出登录
    * @Date: 2021/8/25
    **/
    @RequestMapping("/loginOut")
    public ResponseData loginOut(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse){
        Cookie cookie = CookieUtil.genCookie(TokenIntercepter.ACCESS_TOKEN, "", "/", 0);
        httpServletResponse.addCookie(cookie);
        return new ResponseUtil<>().setData(null);
    }
    /**
     * @Description: 用户注册激活
     * @Date: 2021/8/25
     **/
    @Anoymous
    @RequestMapping("/verify")
    public ResponseData verify(String uuid,String username){
        UserVerifyRequest userVerifyRequest = new UserVerifyRequest();
        userVerifyRequest.setUuid(uuid);
        userVerifyRequest.setUserName(username);
        userRegisterService.userRegisterActivate(userVerifyRequest);
        return new ResponseUtil<>().setData(null);
    }
}
